﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotSpatial.Controls;
using HydroDesktop.Database;
using HydroDesktop.Configuration;
using HydroDesktop.Interfaces;

namespace HydroDesktop.Main
{
    public partial class ChangeDatabaseForm : Form
    {
        private string _oldDbFileName;
        private string _oldConnectionString;
        private string _newConnectionString;
        private AppManager _appManager;

        public ChangeDatabaseForm(AppManager appManager)
        {
            InitializeComponent();
            LoadSettings();
            //_newConnectionString = Config.DataRepositoryConnectionString;
            //_oldConnectionString = _newConnectionString;
            //txtDatabaseFile.Text = GetDbFileName(Config.DataRepositoryConnectionString);
            //_oldDbFileName = txtDatabaseFile.Text;
            _appManager = appManager;
        }

        private void LoadSettings()
        {
            //TODO replace Config by Settings or AppManager
            
            ////Settings.Instance.Load();
            _newConnectionString = Settings.Instance.DataRepositoryConnectionString;
            _oldConnectionString = _newConnectionString;

            _oldDbFileName = SQLiteHelper.GetSQLiteFileName(Settings.Instance.DataRepositoryConnectionString);

            //when the db file does not exist --> try to use default database instead.
            if (System.IO.File.Exists(_oldDbFileName) == false)
            {
                //_newConnectionString = Config.DataRepositoryConnectionString;
                //_oldConnectionString = _newConnectionString;
                //Settings.Instance.DataRepositoryConnectionString = _newConnectionString;
            }

            txtDatabaseFile.Text = SQLiteHelper.GetSQLiteFileName(Settings.Instance.DataRepositoryConnectionString);
            _oldDbFileName = txtDatabaseFile.Text;
        }

        private void SaveSettings()
        {
            //Settings.Instance.Save();
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "SQLite databases|*.sqlite";
            fileDialog.FileName = txtDatabaseFile.Text;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                _newConnectionString = SQLiteHelper.GetSQLiteConnectionString(fileDialog.FileName);
                txtDatabaseFile.Text = fileDialog.FileName;  
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            //Config.ChangeDataRepository(_oldConnectionString);
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //TODO replace config by calling AppManager
            
            //Config.ChangeDataRepository(_newConnectionString);

            //if (Config.DefaultDataRepositoryOperations.TestConnection() == false)
            //{
            //    MessageBox.Show("Error connecting to database. The database connection string is not valid.");
            //    Config.ChangeDataRepository(_oldConnectionString);
            //    txtDatabaseFile.Text = SQLiteHelper.GetSQLiteFileName(Config.DataRepositoryConnectionString);
            //}
            //else
            //{
            //    this.Cursor = Cursors.WaitCursor;

            //    Settings.Instance.DataRepositoryConnectionString = _newConnectionString;
            //    SaveSettings();

            //    if (this.Owner is mainRibbonForm)
            //    {
            //        mainRibbonForm ownerForm = this.Owner as mainRibbonForm;
            //        ownerForm.RefreshAllThemes();
            //    }

            //    //to restore the SeriesSelector
            //    _appManager.Database = new HydroDatabase(Settings.Instance.DataRepositoryConnectionString);
            //    _appManager.SeriesView.SeriesSelector.RefreshSelection();

            //    this.Cursor = Cursors.Default;
            //    MessageBox.Show("Database changed successfully.");
            //    DialogResult = DialogResult.OK;
            //    Close();
            //}
        }

        private void btnRestoreDefault_Click(object sender, EventArgs e)
        {
            //TODO replace config by AppManager or Settings
            
            //txtDatabaseFile.Text = SQLiteHelper.GetSQLiteFileName(Config.DefaultActualDataConnection());
            //_newConnectionString = SQLiteHelper.GetSQLiteConnectionString(txtDatabaseFile.Text);
        }
    }
}
